package com.nanyang.tms.service.export;

import com.alibaba.fastjson.JSONObject;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.export.ExportDataRequest;
import com.nanyang.tms.api.service.export.TmsExportDataRpcService;
import com.nanyang.tms.domain.export.ExportDataVO;
import com.nanyang.tms.domain.waybill.WaybillQuery;
import com.nanyang.tms.manager.export.ExportRelatedManager;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p></p>
 * <p>Description:</p>
 *
 * @author: bwl
 * date: 2020/12/8 9:23
 * @Copyright: 2020 www.rongetong.cn Inc. All rights reserved.
 */
@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0)
public class TmsExportDataServiceImpl implements TmsExportDataRpcService {

    @Autowired
    private ExportRelatedManager exportRelatedManager;
    @Override
    public ApiResult<Boolean> exportDataMq(ExportDataRequest exportDataRequest) {
        ApiResult<Boolean> result = new ApiResult<>();
        ExportDataVO exportDataVO = new ExportDataVO();
        try {
            exportDataRequest.requestCheck();
            BeanUtils.copyProperties(exportDataRequest, exportDataVO);
            exportRelatedManager.exportDataMq(exportDataVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<String> exportData(ExportDataRequest exportDataRequest) {
        ApiResult<String> result = new ApiResult<>();
        exportDataRequest.requestCheck();
        try {
            String fileUrl = "";
             if ("waybillData".equals(exportDataRequest.getBusinessType())) {
                 WaybillQuery WaybillQuery = new WaybillQuery();
                if (StringUtil.isNotEmpty(exportDataRequest.getFilterJson())) {
                    WaybillQuery = JSONObject.parseObject(exportDataRequest.getFilterJson(), WaybillQuery.class);
                }
                fileUrl = exportRelatedManager.waybillExportData(WaybillQuery);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(fileUrl);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
